//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <buola/buola.h>
#include <buola/datetime/cdatetime.h>
#include <buola/io/curi.h>

#include <cwchar>
#include <cstdlib>
#include <ctime>

namespace buola {
    
namespace detail {
    
    template<typename tInt>
    inline std::string to_string_uint(tInt pInt,size_t pWidth,char pFill)
    {
        return std::to_string((uint64_t)pInt);
    }
    
    template<typename tInt>
    inline std::string to_string_int(tInt pInt,size_t pWidth,char pFill)
    {
        char lBuffer[32];
        bool lSign=false;

        if(pInt<0)
        {
            lSign=true;
            pInt=-pInt;
        }
        
        int lPos=32;
        if(!pInt)
        {
            lBuffer[--lPos]='0';
        }
        else do
        {
            lBuffer[--lPos]='0'+(pInt%10);
            pInt/=10;
        } while(pInt);
        
        if(lSign)
            lBuffer[--lPos]='-';
        
        return std::string(lBuffer+lPos,lBuffer+32);
    }
    
    template<typename tReal>
    inline std::string to_string_real(tReal pReal,size_t pPrecision)
    {
        return std::to_string(pReal);
    }
    
    template<typename tInt>
    inline std::wstring to_wstring_uint(tInt pInt,size_t pWidth,char pFill)
    {
        return std::to_wstring((uint64_t)pInt);
    }
    
    template<typename tInt>
    inline std::wstring to_wstring_int(tInt pInt,size_t pWidth,char pFill)
    {
        return std::to_wstring(pInt);
    }
    
    template<typename tReal>
    inline std::wstring to_wstring_real(tReal pReal,size_t pPrecision)
    {
        return std::to_wstring(pReal);
    }
    
/*namespace detail*/ }
    
///////////////////////////////////// integral types ///////////////////////////////////

std::string to_string(uint8_t pInt,size_t pWidth,char pFill)
{
    return detail::to_string_uint(pInt,pWidth,pFill);
}

std::string to_string(int8_t pInt,size_t pWidth,char pFill)
{
    return detail::to_string_int(pInt,pWidth,pFill);
}

std::string to_string(uint16_t pInt,size_t pWidth,char pFill)
{
    return detail::to_string_uint(pInt,pWidth,pFill);
}

std::string to_string(int16_t pInt,size_t pWidth,char pFill)
{
    return detail::to_string_int(pInt,pWidth,pFill);
}

std::string to_string(uint32_t pInt,size_t pWidth,char pFill)
{
    return detail::to_string_uint(pInt,pWidth,pFill);
}

std::string to_string(int32_t pInt,size_t pWidth,char pFill)
{
    return detail::to_string_int(pInt,pWidth,pFill);
}

std::string to_string(uint64_t pInt,size_t pWidth,char pFill)
{
    return detail::to_string_uint(pInt,pWidth,pFill);
}

std::string to_string(int64_t pInt,size_t pWidth,char pFill)
{
    return detail::to_string_int(pInt,pWidth,pFill);
}

std::string to_string(float pReal,size_t pPrecision)
{
    return detail::to_string_real(pReal,pPrecision);
}

std::string to_string(double pReal,size_t pPrecision)
{
    return detail::to_string_real(pReal,pPrecision);
}

std::string to_string(long double pReal,size_t pPrecision)
{
    return detail::to_string_real(pReal,pPrecision);
}

std::wstring to_wstring(uint8_t pInt,size_t pWidth,char pFill)
{
    return detail::to_wstring_uint(pInt,pWidth,pFill);
}

std::wstring to_wstring(int8_t pInt,size_t pWidth,char pFill)
{
    return detail::to_wstring_int(pInt,pWidth,pFill);
}

std::wstring to_wstring(uint16_t pInt,size_t pWidth,char pFill)
{
    return detail::to_wstring_uint(pInt,pWidth,pFill);
}

std::wstring to_wstring(int16_t pInt,size_t pWidth,char pFill)
{
    return detail::to_wstring_int(pInt,pWidth,pFill);
}

std::wstring to_wstring(uint32_t pInt,size_t pWidth,char pFill)
{
    return detail::to_wstring_uint(pInt,pWidth,pFill);
}

std::wstring to_wstring(int32_t pInt,size_t pWidth,char pFill)
{
    return detail::to_wstring_int(pInt,pWidth,pFill);
}

std::wstring to_wstring(uint64_t pInt,size_t pWidth,char pFill)
{
    return detail::to_wstring_uint(pInt,pWidth,pFill);
}

std::wstring to_wstring(int64_t pInt,size_t pWidth,char pFill)
{
    return detail::to_wstring_int(pInt,pWidth,pFill);
}

std::wstring to_wstring(float pReal,size_t pPrecision)
{
    return detail::to_wstring_real(pReal,pPrecision);
}

std::wstring to_wstring(double pReal,size_t pPrecision)
{
    return detail::to_wstring_real(pReal,pPrecision);
}

std::wstring to_wstring(long double pReal,size_t pPrecision)
{
    return detail::to_wstring_real(pReal,pPrecision);
}

template<>
int8_t to_<int8_t>(const std::string &pStr)
{
    return (int8_t)std::stol(pStr);
}

template<>
uint8_t to_<uint8_t>(const std::string &pStr)
{
    return (uint8_t)std::stoul(pStr);
}

template<>
int16_t to_<int16_t>(const std::string &pStr)
{
    return (int16_t)std::stol(pStr);
}

template<>
uint16_t to_<uint16_t>(const std::string &pStr)
{
    return (uint16_t)std::stoul(pStr);
}

template<>
int32_t to_<int32_t>(const std::string &pStr)
{
    return (int32_t)std::stol(pStr);
}

template<>
uint32_t to_<uint32_t>(const std::string &pStr)
{
    return (uint32_t)std::stoul(pStr);
}

template<>
int64_t to_<int64_t>(const std::string &pStr)
{
    return (int64_t)std::stol(pStr);
}

template<>
uint64_t to_<uint64_t>(const std::string &pStr)
{
    return (uint64_t)std::stoul(pStr);
}

template<>
int8_t to_<int8_t>(const std::wstring &pStr)
{
    return (int8_t)std::stol(pStr);
}

template<>
uint8_t to_<uint8_t>(const std::wstring &pStr)
{
    return (uint8_t)std::stoul(pStr);
}

template<>
int16_t to_<int16_t>(const std::wstring &pStr)
{
    return (int16_t)std::stol(pStr);
}

template<>
uint16_t to_<uint16_t>(const std::wstring &pStr)
{
    return (uint16_t)std::stoul(pStr);
}

template<>
int32_t to_<int32_t>(const std::wstring &pStr)
{
    return (int32_t)std::stol(pStr);
}

template<>
uint32_t to_<uint32_t>(const std::wstring &pStr)
{
    return (uint32_t)std::stoul(pStr);
}

template<>
int64_t to_<int64_t>(const std::wstring &pStr)
{
    return (int64_t)std::stol(pStr);
}

template<>
uint64_t to_<uint64_t>(const std::wstring &pStr)
{
    return (uint64_t)std::stoul(pStr);
}

#if BUOLA_PLATFORM_MAC

template<>
size_t to_<size_t>(const std::string &pStr)
{
    return (size_t)std::stoul(pStr);
}

std::string to_string(const size_t &p64)
{
    return std::to_string(p64);
}

#endif //BUOLA_PLATFORM_MAC

/////////////////////////////// floating point types /////////////////////////////

template<>
double to_<double>(const std::string &pStr)
{
    return std::stod(pStr);
}

template<>
float to_<float>(const std::string &pStr)
{
    return std::stof(pStr);
}

template<>
long double to_<long double>(const std::string &pStr)
{
    return std::stold(pStr);
}

template<>
double to_<double>(const std::wstring &pStr)
{
    return std::stod(pStr);
}

template<>
float to_<float>(const std::wstring &pStr)
{
    return std::stof(pStr);
}

template<>
long double to_<long double>(const std::wstring &pStr)
{
    return std::stold(pStr);
}

////////////////////////////////// special types /////////////////////////////////

///////////////////////////////////////////////
// wchar_t (this is converted as a character)
//

template<>
wchar_t to_<wchar_t>(const std::string &pStr)
{
    return utf32(pStr)[0];
}

std::string to_string(const wchar_t &pChar)
{
    std::wstring lRet;
    lRet+=pChar;
    return utf8(lRet);
}

template<>
wchar_t to_<wchar_t>(const std::wstring &pStr)
{
    return pStr[0];
}

std::wstring to_wstring(const wchar_t &pChar)
{
    std::wstring lRet;
    lRet+=pChar;
    return lRet;
}

//////////
// bool
//

template<>
bool to_<bool>(const std::string &pStr)
{
    if(!pStr.length()) return false;
    
    if(pStr[0]=='f'||pStr[0]=='F'||pStr[0]=='n'||
        pStr[0]=='N'||pStr=="0")
    {
        return false;
    }
    else
    {
        return true;
    }
}

std::string to_string(bool pBool)
{
    if(pBool)
        return "true";
    else
        return "false";
}

std::wstring to_wstring(bool pBool)
{
    if(pBool)
        return L"true";
    else
        return L"false";
}

/////////////////////////////////////////////////////////////////////////////
// void* (printed as an hexadecimal number, no conversion back to pointer)
//

template<>
void* to_<void*>(const std::string& /*pStr*/)
{
    return nullptr;
}

std::string to_string(void * const &pPointer)
{
    return str_printf("%p",pPointer);
}

///////////////////////////////// datetime classes ////////////////////////////////
    
///////////
// CTime
    
template<>
CCalendarTime to_<CCalendarTime>(const std::string &pStr)
{
    struct tm lTM;

    strptime(pStr.c_str(),"%T %F %z",&lTM);

    return CCalendarClock::from_time_t(mktime(&lTM));
}

std::string to_string(const CCalendarTime &pTime)
{
    return utf8(CDateTime(pTime).GetISODateTime());
}

////////////////////////////////// io classes ////////////////////////////////////

//////////
// CURL
//

template<>
io::CURI to_<io::CURI>(const std::string &pStr)
{
    return io::uri(pStr);
}

std::string to_string(const io::CURI &pURI)
{
    return pURI.ToString();
}

/*namespace buola*/ }
